/*
 *  Copyright (c) 2015. For Intelligent Group.
 *
 */

package com.intelligent.ispc.client.core.validator;

import javax.swing.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lucien.Chen on 2016/9/1.
 */

public class LangBoxModel<E> extends AbstractListModel<E> implements MutableComboBoxModel<E>, Serializable {
    private List<E> list;
    Object selectedObject;

    public LangBoxModel() {
        list = new ArrayList<E>();
    }

    public LangBoxModel(final E items[]) {
        list = new ArrayList<E>(items.length);

        int i, c;
        for (i = 0, c = items.length; i < c; i++)
            list.add(items[i]);

        if (getSize() > 0) {
            selectedObject = getElementAt(0);
        }
    }

    public LangBoxModel(final List<E> items) {
        list = items;
        if (getSize() > 0) {
            selectedObject = getElementAt(0);
        }
    }

    @Override
    public void addElement(E item) {

    }

    @Override
    public void removeElement(Object obj) {

    }

    @Override
    public void insertElementAt(E item, int index) {

    }

    @Override
    public void removeElementAt(int index) {

    }

    @Override
    public void setSelectedItem(Object anItem) {
        if ((selectedObject != null && !selectedObject.equals(anItem)) || selectedObject == null && anItem != null) {
            selectedObject = anItem;
            fireContentsChanged(this, -1, -1);
        }
    }

    @Override
    public Object getSelectedItem() {
        return selectedObject;
    }

    @Override
    public int getSize() {
        return list.size();
    }

    @Override
    public E getElementAt(int index) {
        return list.get(index);
    }
}

